﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace IOTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          //设置一个定时器，几秒钟内将当前目录下的所有文件添加到一个List中
          //然后处理
          //放在数据库中
          //再定时刷新GridView
           
        }

        public void Read(string path)
        {
            StreamReader sr = new StreamReader(path, Encoding.Default);
            String line;
            List<String> ListString = new List<String>();
            while ((line = sr.ReadLine()) != null)
            {
                ListString.Add(line);
            }


            if (ListString.Count == 67)
            {
                Model model = initDataNOK(ListString);
                SaveData(model);

            }
            else if (ListString.Count == 63)
            {
                Model model = initDataNOK(ListString);
                SaveData(model);
            }
            else
            {
                MessageBox.Show("该文件的格式不正确！");
                return;
            }

        }
        /// <summary>
        /// 这个是结果ok的
        /// </summary>
        /// <param name="strList"></param>
        public Model initDataOK(List<String> strList)
        {
            Model model = new Model();
            model.Res = strList[3].Substring(25, 3);
            Console.WriteLine("model.Res:" + model.Res);

            model.Time = strList[2].Substring(2, 10) + " " + strList[2].Substring(15, 8);
            Console.WriteLine("model.Times:" + model.Time);

            model.ChRes1 = strList[6].Substring(0, 2);
            Console.WriteLine("model.ChRes1:" + model.ChRes1);

            model.ChRes2 = strList[7].Substring(0, 2);
            Console.WriteLine("model.ChRes2:" + model.ChRes2);

            model.ChRes3 = strList[8].Substring(0, 2);
            Console.WriteLine("model.ChRes3:" + model.ChRes3);

            model.ChRes4 = strList[9].Substring(0, 2);
            Console.WriteLine("model.ChRes4:" + model.ChRes4);

            //ch1
            model.Ch1Force = strList[15].Substring(20, 5);
            Console.WriteLine("model.Ch1Force:" + model.Ch1Force);
            model.Ch1Angle = strList[16].Substring(20, 5);
            Console.WriteLine("model.Ch2Angle:" + model.Ch1Angle);

            //ch2
            model.Ch2Force = strList[29].Substring(20, 5);
            Console.WriteLine("model.Ch2Force:" + model.Ch2Force);
            model.Ch2Angle = strList[30].Substring(20, 5);
            Console.WriteLine("model.Ch2Angle:" + model.Ch2Angle);

            //ch3
            model.Ch3Force = strList[43].Substring(20, 5);
            Console.WriteLine("model.Ch3Force:" + model.Ch3Force);
            model.Ch3Angle = strList[44].Substring(20, 5);
            Console.WriteLine("model.Ch3Angle:" + model.Ch3Angle);

            ////ch4
            model.Ch4Force = strList[57].Substring(20, 5);
            Console.WriteLine("model.Ch4Force:" + model.Ch4Force);
            model.Ch4Angle = strList[58].Substring(20, 5);
            Console.WriteLine("model.Ch4Angle:" + model.Ch4Angle);

            return model;
        }

        /// <summary>
        /// Not OK 
        /// </summary>
        /// <param name="strList"></param>
        public Model initDataNOK(List<String> strList)
        {
            Model model = new Model();
            model.Res = strList[3].Substring(26, 3);
            Console.WriteLine("model.Res:" + model.Res);

            model.Time = strList[2].Substring(2, 10) + " " + strList[2].Substring(15, 8);
            Console.WriteLine("model.Times:" + model.Time);

            model.ChRes1 = strList[6].Substring(0, 3);
            Console.WriteLine("model.ChRes1:" + model.ChRes1);

            model.ChRes2 = strList[7].Substring(0, 3);
            Console.WriteLine("model.ChRes2:" + model.ChRes2);

            model.ChRes3 = strList[8].Substring(0, 3);
            Console.WriteLine("model.ChRes3:" + model.ChRes3);

            model.ChRes4 = strList[9].Substring(0, 3);
            Console.WriteLine("model.ChRes4:" + model.ChRes4);

            //ch1
            model.Ch1Force = strList[15].Substring(20, 5);
            Console.WriteLine("model.Ch1Force:" + model.Ch1Force);
            model.Ch1Angle = strList[16].Substring(20, 5);
            Console.WriteLine("model.Ch1Angle:" + model.Ch1Angle);

            //ch2
            model.Ch2Force = strList[28].Substring(20, 5);
            Console.WriteLine("model.Ch2Force:" + model.Ch2Force);
            model.Ch2Angle = strList[29].Substring(20, 5);
            Console.WriteLine("model.Ch2Angle:" + model.Ch2Angle);

            //ch3
            model.Ch3Force = strList[41].Substring(20, 5);
            Console.WriteLine("model.Ch3Force:" + model.Ch3Force);
            model.Ch3Angle = strList[42].Substring(20, 5);
            Console.WriteLine("model.Ch3Angle:" + model.Ch3Angle);

            //////ch4
            model.Ch4Force = strList[54].Substring(20, 5);
            Console.WriteLine("model.Ch4Force:" + model.Ch4Force);
            model.Ch4Angle = strList[55].Substring(20, 5);
            Console.WriteLine("model.Ch4Angle:" + model.Ch4Angle);

            return model;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="model"></param>
        public void SaveData(Model model) {
            string sqlConn = " Data Source =.; Initial Catalog = IoTest; Integrated Security = True; Pooling = False";
            SqlConnection MyConnection = new SqlConnection(sqlConn);
            string sql = "insert into T_Model(F_Res,F_Time,F_Ch1Res,F_Ch2Res,F_Ch3Res,F_Ch4Res,F_Ch1Force,F_Ch2Force,F_Ch3Force,F_Ch4Force,F_Ch1Angle,F_Ch2Angle,F_Ch3Angle,F_Ch4Angle)" +
                "Values('" + model.Res + "','" + model.Time +"','" + model.ChRes1 + "','" + model.ChRes2 + "','" + model.ChRes3 + "','" + model.ChRes4 + "','" + model.Ch1Force + "','" + model.Ch2Force + "','" + model.Ch3Force + "','" + model.Ch4Force + "','" + model.Ch1Angle + "','" + model.Ch2Angle + "','" + model.Ch3Angle + "','" + model.Ch4Angle + "')";
            SqlCommand MyCommand = new SqlCommand(sql, MyConnection);
            try
            {
                MyConnection.Open();
                if (MyCommand.ExecuteNonQuery() == 1)
                {
                    Console.WriteLine("成功插入一条时间为" + model.Time + "和结果为" + model.Res + "的数据");
                }
                MyConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }

        /// <summary>
        /// 按时间查询
        /// 返回一个Dataset
        /// </summary>
        /// <param name="time"></param>
        public DataTable GetTableByTime(string time1,string time2)
        {
            string sqlConn = " Data Source =.; Initial Catalog = IoTest; Integrated Security = True; Pooling = False";
            SqlConnection MyConnection = new SqlConnection(sqlConn);
            string sql = "select * from T_Model where F_Time between '"+time1+" 00:00:00' and '"+time2+" 23:59:59'";
            SqlDataAdapter sda = new SqlDataAdapter(sql,sqlConn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

        public DataTable GetTodayTable()
        {
            string sqlConn = " Data Source =.; Initial Catalog = IoTest; Integrated Security = True; Pooling = False";
            SqlConnection MyConnection = new SqlConnection(sqlConn);
            string sql = "select * from T_Model where F_Time between '"+ DateTime.Now.ToShortDateString() + " 00:00:00'  and '"+ DateTime.Now.ToShortDateString() + " 23:59:59'";
            SqlDataAdapter sda = new SqlDataAdapter(sql, sqlConn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

        public void Read1(string path)
        {
            byte[] byData = new byte[100];
            char[] charData = new char[1000];
            string str1 = "";
            string str2 = "";
            string str3 = "";
            try
            {
                FileStream file = new FileStream(path, FileMode.Open);
                file.Seek(0, SeekOrigin.Begin);//寻找文件
                file.Read(byData, 0, 100); //byData传进来的字节数组,用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.
                Decoder d = Encoding.Default.GetDecoder();
                d.GetChars(byData, 0, byData.Length, charData, 0);
                Console.WriteLine(charData);
                file.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ////NOK
            string path = "E:/workApp/ata1a/FO/SEQ_04/100/FO_000286328594.txt";

            //OK
            //string path = "E:/workApp/ata1a/FO/SEQ_04/104/FO_000286328612.txt";
            this.Read(path);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
            this.dateTimePicker1.CustomFormat = "yyyy-MM-dd";
            this.dateTimePicker2.Format = DateTimePickerFormat.Custom;
            this.dateTimePicker2.CustomFormat = "yyyy-MM-dd";
            dataGridView1.DataSource = GetTableByTime(dateTimePicker1.Text.ToString(), dateTimePicker2.Text.ToString());
         
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = GetTodayTable();
        }

        public void initFile() {
            //使用file类的list
            //

        }
    }
}
